<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce"> 
							<el-col v-if="$check_field('get','shop_name') || $check_field('add','shop_name') || $check_field('set','shop_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="店铺名称" prop="shop_name">
															<el-input id="shop_name" v-model="form['shop_name']" placeholder="请输入店铺名称"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','shop_name')) || (!form['maintenance_appointment_id'] && $check_field('add','shop_name'))" :disabled="disabledObj['shop_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','shop_name')">{{form['shop_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','detailed_address') || $check_field('add','detailed_address') || $check_field('set','detailed_address')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="详细地址" prop="detailed_address">
															<el-input id="detailed_address" v-model="form['detailed_address']" placeholder="请输入详细地址"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','detailed_address')) || (!form['maintenance_appointment_id'] && $check_field('add','detailed_address'))" :disabled="disabledObj['detailed_address_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','detailed_address')">{{form['detailed_address']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_scope') || $check_field('add','business_scope') || $check_field('set','business_scope')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="经营范围" prop="business_scope">
															<el-input id="business_scope" v-model="form['business_scope']" placeholder="请输入经营范围"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','business_scope')) || (!form['maintenance_appointment_id'] && $check_field('add','business_scope'))" :disabled="disabledObj['business_scope_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','business_scope')">{{form['business_scope']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_user') || $check_field('add','business_user') || $check_field('set','business_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家用户" prop="business_user">
																<el-select v-if="(form['maintenance_appointment_id'] && $check_field('set','business_user')) || (!form['maintenance_appointment_id'] && $check_field('add','business_user'))" id="business_user" v-model="form['business_user']" :disabled="disabledObj['business_user_isDisabled']">
							<el-option v-for="o in list_user_business_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','business_user')" id="business_user" v-model="form['business_user']" :disabled="true">
							<el-option v-for="o in list_user_business_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_name') || $check_field('add','business_name') || $check_field('set','business_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家姓名" prop="business_name">
															<el-input id="business_name" v-model="form['business_name']" placeholder="请输入商家姓名"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','business_name')) || (!form['maintenance_appointment_id'] && $check_field('add','business_name'))" :disabled="disabledObj['business_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','business_name')">{{form['business_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','business_phone') || $check_field('add','business_phone') || $check_field('set','business_phone')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="商家电话" prop="business_phone">
															<el-input id="business_phone" v-model="form['business_phone']" placeholder="请输入商家电话"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','business_phone')) || (!form['maintenance_appointment_id'] && $check_field('add','business_phone'))" :disabled="disabledObj['business_phone_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','business_phone')">{{form['business_phone']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','member_user') || $check_field('add','member_user') || $check_field('set','member_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="会员用户" prop="member_user">
																					<div v-if="user_group !== '管理员'">
							{{ get_user_session_member_user(form['member_user']) }}
							<el-select v-if="(form['maintenance_appointment_id'] && $check_field('set','member_user')) || (!form['maintenance_appointment_id'] && $check_field('add','member_user'))" id="member_user" v-model="form['member_user']" :disabled="disabledObj['member_user_isDisabled']">
								<el-option v-for="o in list_user_member_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','member_user')" id="member_user" v-model="form['member_user']" :disabled="true">
								<el-option v-for="o in list_user_member_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="member_user" v-model="form['member_user']" :disabled="disabledObj['member_user_isDisabled']">
							<el-option v-for="o in list_user_member_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','user_name') || $check_field('add','user_name') || $check_field('set','user_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="用户姓名" prop="user_name">
															<el-input id="user_name" v-model="form['user_name']" placeholder="请输入用户姓名"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','user_name')) || (!form['maintenance_appointment_id'] && $check_field('add','user_name'))" :disabled="disabledObj['user_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','user_name')">{{form['user_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','contact_information') || $check_field('add','contact_information') || $check_field('set','contact_information')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="联系方式" prop="contact_information">
															<el-input id="contact_information" v-model="form['contact_information']" placeholder="请输入联系方式"
							  v-if="(form['maintenance_appointment_id'] && $check_field('set','contact_information')) || (!form['maintenance_appointment_id'] && $check_field('add','contact_information'))" :disabled="disabledObj['contact_information_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','contact_information')">{{form['contact_information']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','appointment_date') || $check_field('add','appointment_date') || $check_field('set','appointment_date')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="预约日期" prop="appointment_date">
											<el-date-picker :disabled="disabledObj['appointment_date_isDisabled']" v-if="(form['maintenance_appointment_id'] && $check_field('set','appointment_date')) || (!form['maintenance_appointment_id'] && $check_field('add','appointment_date'))" id="appointment_date"
						v-model="form['appointment_date']" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
					</el-date-picker>
					<div v-else-if="$check_field('get','appointment_date')">{{form['appointment_date']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','appointment_status') || $check_field('add','appointment_status') || $check_field('set','appointment_status')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="预约状态" prop="appointment_status">
											<el-select id="appointment_status" v-model="form['appointment_status']"
						v-if="(form['maintenance_appointment_id'] && $check_field('set','appointment_status')) || (!form['maintenance_appointment_id'] && $check_field('add','appointment_status'))">
						<el-option v-for="o in list_appointment_status" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','appointment_status')">{{form['appointment_status']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','appointment_introduction') || $check_field('add','appointment_introduction') || $check_field('set','appointment_introduction')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="预约简介" prop="appointment_introduction">
											<el-input type="textarea" id="appointment_introduction" v-model="form['appointment_introduction']" placeholder="请输入预约简介"
						v-if="(form['maintenance_appointment_id'] && $check_field('set','appointment_introduction')) || (!form['maintenance_appointment_id'] && $check_field('add','appointment_introduction'))" :disabled="disabledObj['appointment_introduction_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','appointment_introduction')">{{form['appointment_introduction']}}</div>
							</el-form-item>
			</el-col>
						
	
	
		
		
	
	
	
		
		
	
		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/maintenance_appointment/view','set') || $check_action('/maintenance_appointment/view','add')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		data() {
			return {
				field: "maintenance_appointment_id",
				url_add: "~/api/maintenance_appointment/add?",
				url_set: "~/api/maintenance_appointment/set?",
				url_get_obj: "~/api/maintenance_appointment/get_obj?",
				url_upload: "~/api/maintenance_appointment/upload?",

				query: {
					"maintenance_appointment_id": 0,
				},

				form: {
								"shop_name":  '', // 店铺名称
										"detailed_address":  '', // 详细地址
										"business_scope":  '', // 经营范围
										"business_user": 0, // 商家用户
										"business_name":  '', // 商家姓名
										"business_phone":  '', // 商家电话
										"member_user": 0, // 会员用户
										"user_name":  '', // 用户姓名
										"contact_information":  '', // 联系方式
										"appointment_date":  '', // 预约日期
										"appointment_status":  '', // 预约状态
										"appointment_introduction":  '', // 预约简介
											"maintenance_appointment_id": 0, // ID
															},
				disabledObj:{
								"shop_name_isDisabled": false,
										"detailed_address_isDisabled": false,
										"business_scope_isDisabled": false,
										"business_user_isDisabled": false,
										"business_name_isDisabled": false,
										"business_phone_isDisabled": false,
										"member_user_isDisabled": false,
										"user_name_isDisabled": false,
										"contact_information_isDisabled": false,
										"appointment_date_isDisabled": false,
										"appointment_status_isDisabled": false,
										"appointment_introduction_isDisabled": false,
										},

	
			
			
			
					// 用户列表
				list_user_business_user: [],
					
			
			
					// 用户列表
				list_user_member_user: [],
						// 用户组
				group_user_member_user: "",
					
			
			
									// 预约状态选项列表
				list_appointment_status: ['成功','失败','待安排'],
	
			
		
			}
		},
		methods: {

	
	
			
	
			
	
			
	
				/**
			 * 获取商家用户用户列表
			 */
			async get_list_user_business_user() {
                var json = await this.$get("~/api/user/get_list?user_group=商家用户");
                if(json.result && json.result.list){
                    this.list_user_business_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					get_user_business_user(id){
				var obj = this.list_user_business_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
				/**
			 * 获取会员用户用户列表
			 */
			async get_list_user_member_user() {
                var json = await this.$get("~/api/user/get_list?user_group=会员用户");
                if(json.result && json.result.list){
                    this.list_user_member_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取会员用户用户组
			 */
			async get_group_user_member_user() {
							this.form["member_user"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=会员用户");
				if(json.result && json.result.obj){
					this.group_user_member_user = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_member_user(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_member_user.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["member_user"] = id
									_this.disabledObj['member_user' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "member_user") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_member_user(id){
				var obj = this.list_user_member_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
															// 获取缓存数据附加
				form = $.db.get("form");
									$.push(this.form ,form);
								
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
										        if (this.form["appointment_date"] && JSON.stringify(this.form["appointment_date"]).indexOf("-")===-1){
          this.form["appointment_date"] = this.$toTime(parseInt(this.form["appointment_date"]),"yyyy-MM-dd")
        }
							$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
																																																			if(this.form["appointment_date"]=="0000-00-00"){
				  this.form["appointment_date"] = null;
				}
				if(parseInt(this.form["appointment_date"]) > 9999){
					this.form["appointment_date"] = this.$toTime(parseInt(this.form["appointment_date"]),"yyyy-MM-dd")
				}
														

			},

																																																																																																async submit(param, func){
				if (!param) {
					param = this.form;
				}
						
				var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																															ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
					
																																																																																																										if (!param.appointment_date){
					return "预约日期不能为空";
				}
																																						return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/maintenance_appointment/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/maintenance_appointment/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/maintenance_appointment/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/maintenance_appointment/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/maintenance_appointment/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
											this.get_list_user_business_user();
												this.get_list_user_member_user();
					this.get_group_user_member_user();
															},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




	
</style>
